if(typeof Ext.user.method.didstore == 'undefined'){ Ext.user.method.didstore = Ext.user.method.did(); }
if(typeof Ext.user.method.roleidstorea == 'undefined'){ Ext.user.method.roleidstorea = Ext.user.method.roleid(); }
Ext.user.userwindow = Ext.extend(Ext.Window, {
    title: '添加用户',
    frame: true,
	id:'userwindow',
	height: 395,
	width:550,
	modal:true,
    initComponent: function(cfg) {
		this.cfg = cfg || {};		
        this.items = [
            {
                xtype: 'form',
				id:'userForm',
				formId:'userFormid',
                title: '',
                style: '',
                autoWidth: true,
                autoHeight: true,
				border: false,
                items: [
                    {xtype: 'fieldset',title: '账户信息', width: 500, style: 'margin:0px auto;',
                        items: [
                            {
                                xtype: 'panel', layout: 'column',border: false,style: 'margin-bottom:5px;',
                                items: [
                                    {xtype: 'panel',columnWidth: 0.1,border: false, html: '用户：', height: 20,style: 'text-align:center; line-height:20px;'},
                                    {xtype: 'textfield',columnWidth: 0.3,name:'account',id:'account'},
                                    {xtype: 'panel', columnWidth: 0.6,border: false}
                                ]
                            },
                            {xtype: 'panel',layout: 'column', border: false,
                                items: [
                                    {xtype: 'panel',columnWidth: 0.1,border: false,html: '密码：',height: 20,style: 'text-align:center; line-height:20px;'},
                                    {xtype: 'textfield',columnWidth: 0.3,name:'password'},
                                    {xtype: 'panel',title: '',columnWidth: 0.6,border: false}
                                ]
                            }
                        ]
                    },
                    {xtype: 'panel',style: 'margin:5px 5px;', border: false, width: 500},
                    {xtype: 'fieldset', title: '基本信息',width: 500, style: 'margin:0px auto;',
                        items: [
                            {xtype: 'panel', layout: 'column',border: false,style: 'margin-bottom:5px;',items: [{xtype: 'panel',columnWidth: 0.15,border: false, html: '真实姓名：', height: 20,style: 'text-align:center; line-height:20px;'},{name:'nickname',xtype: 'textfield',columnWidth: 0.3},{xtype: 'panel', columnWidth: 0.6,border: false}]},
                            {xtype: 'panel', layout: 'column',border: false,style: 'margin-bottom:5px;',items: [{xtype: 'panel',columnWidth: 0.15,border: false, html: '电子邮箱：', height: 20,style: 'text-align:center; line-height:20px;'},{name:'email',xtype: 'textfield',columnWidth: 0.3},{xtype: 'panel', columnWidth: 0.6,border: false}]},									
                            {xtype: 'panel', layout: 'column',border: false,style: 'margin-bottom:5px;',items: [{xtype: 'panel',columnWidth: 0.15,border: false, html: '所属部门：', height: 20,style: 'text-align:center; line-height:20px;'},
							{xtype: 'combo',
							columnWidth: 0.3,
							name:'did', 
							store: Ext.user.method.didstore,
							displayField:'name',
							valueField:'did',
							hiddenName:'did',
							hiddenValue:'did',
							typeAhead: true,
							editable:false,
							forceSelection: true,
							mode:'local',
							triggerAction: 'all',
							emptyText:'请选择',	
							selectOnFocus:true,
							blankText:'不能为空',
							allowBlank:false,
						},{xtype: 'panel', columnWidth: 0.6,border: false}]},	
                            {xtype: 'panel', layout: 'column',border: false,style: 'margin-bottom:5px;',items: [{xtype: 'panel',columnWidth: 0.15,border: false, html: '职务：', height: 20,style: 'text-align:center; line-height:20px;'},
							{xtype: 'combo',
							columnWidth: 0.3,
							name:'roleid', 
							store: Ext.user.method.roleidstorea,
							displayField:'name',
							valueField:'id',
							hiddenName:'roleid',
							hiddenValue:'id',
							editable:false,
							typeAhead: true,
							forceSelection: true,
							mode: 'local',
							triggerAction: 'all',
							emptyText:'请选择',
							selectOnFocus:true,
							blankText:'不能为空',
							allowBlank:false,
							},{xtype: 'panel', columnWidth: 0.6,border: false}]},
							{xtype: 'panel', layout: 'column',border: false,style: 'margin-bottom:5px;',items: [{xtype: 'panel',columnWidth: 0.15,border: false, html: '数据显示区域：', height: 20,style: 'text-align:center; line-height:20px;'},
							{name:'datatype',xtype: 'radio',columnWidth: 0.1 ,boxLabel: '全部',value:1,inputValue:1},
							{name:'datatype',xtype: 'radio',columnWidth: 0.12 ,boxLabel: '本部门',value:2,inputValue:2},
							{name:'datatype',xtype: 'radio',columnWidth: 0.1 ,boxLabel: '自己',value:3,inputValue:3},	
							{xtype: 'panel', columnWidth: 0.6,border: false}]},
                            {xtype: 'panel', layout: 'column',border: false,style: 'margin-bottom:5px;',items: [{xtype: 'panel',columnWidth: 0.15,border: false, html: '备注信息：', height: 20,style: 'text-align:center; line-height:20px;'},{name:'info',xtype: 'textarea',columnWidth: 0.6},{xtype: 'panel', columnWidth: 0.3,border: false}]}
						]		
					},
					{xtype: 'panel',style: 'margin:5px 5px;',border: false,width: 500},
                ]
            }
        ];
		this.button();
        Ext.user.userwindow.superclass.initComponent.call(this);
		
    },
	button:function(){
		Ext.apply(this,this.cfg);
		if(typeof this.edit == 'undefined'){
			this.buttons = [{text:'保存',handler:function(){
				Ext.user.method.submitForm('userForm','/User/add/',{s:function(){
					Ext.getCmp('userwindow').close();
					Ext.StoreMgr.lookup('UserStore').load();
				},f:function(){
					alert('添加失败');
				}},'POST','提交中。。。');
			}},{text:'重置',handler:function(){
				Ext.getCmp('userForm').form.reset();
			}}];
		}else{
			this.buttons = [{text:'修改',handler:function(){
				Ext.user.method.submitForm('userForm','/User/edit/',{s:function(){
					Ext.getCmp('userwindow').close();
					Ext.StoreMgr.lookup('UserStore').load();
				},f:function(){
					alert('修改失败');
				}},'POST','提交中。。。');
					
			}},{text:'重置',handler:function(){
				Ext.getCmp('userForm').form.reset();
			}}];		
		}	
	}
});


//用户字段
Ext.user.usercolumn = new Ext.grid.ColumnModel([
	{xtype: 'gridcolumn',header: '编号',dataIndex: 'id',sortable: true,width: 10,align: 'center'},
	{xtype: 'gridcolumn',header: '账号',dataIndex: 'account',sortable: true,width: 15},
	{xtype: 'gridcolumn',header: '电子邮箱',sortable: true,width: 20,dataIndex: 'email'},
	{xtype: 'gridcolumn', header: '所在部门',dataIndex: 'did',sortable: true,width: 15,renderer:Ext.user.method.userrole},
	{xtype: 'gridcolumn', header: '角色',dataIndex: 'job',sortable: true,width: 10},
	{xtype: 'gridcolumn', header: '最后登录时间',dataIndex: 'last_login_time',sortable: true,width: 20,renderer:Ext.user.method.date},
	{xtype: 'gridcolumn', header: '最后登录ip',dataIndex: 'last_login_ip',sortable: true,width: 20},
	{xtype: 'gridcolumn', header: '登录次数',dataIndex: 'login_count',sortable: true,width: 10},
	{xtype: 'gridcolumn', header: '建立时间',dataIndex: 'create_time',sortable: true,width: 20,renderer:Ext.user.method.date},
	{xtype: 'gridcolumn', header: '状态',dataIndex: 'status',sortable: true,width: 10,align: 'center',renderer:Ext.user.method.status,
		editor: {
		xtype: 'combo',
		name: 'status',
		store:Ext.user.method.statusid(),
		displayField:'text',
		valueField:'value',
		editable:false,
		typeAhead: true,
		forceSelection: true,
		mode: 'local',
		triggerAction: 'all',
		selectOnFocus:true
		}
	},
	{xtype: 'gridcolumn', header: '操作',dataIndex: 'id',sortable: true,width: 10,align: 'center',renderer:function(n){return '<a href="javascript:Ext.user.deleteusergrid('+n+');">删除</a>'}}
]);
//用户列表
Ext.user.userdatalist = Ext.extend(Ext.Panel, { 
    border: false,
	renderTo:'user_index',
    initComponent: function() {
        this.items = [
            {
                border: false,
				id:'userlisteditgrid',
				clicksToEdit:1,
				xtype: 'editorgrid', 
				title: '用户列表', 
				store: 'UserStore', 
				height:window.parent.Ext.getCmp('center_7').getHeight() - 28, 
				loadMask: true,
				trackMouseOver:true,
				columnLines:true,
				cm: Ext.user.usercolumn,
				sm: new Ext.grid.RowSelectionModel({ 
					singleSelect: true//如果不想强制每次只能选一行，可以把这句代码注释掉 
				}),
				tbar: [{text: '添加用户',iconCls: 'silk-add',handler:function(){
					var user = new Ext.user.userwindow();
					user.show();	
					}
				}
		],
			listeners:{
				rowdblclick:function(g, n, e){
					var user = new Ext.user.userwindow({edit:true});
					user.setTitle('修改信息');
					user.show();
					Ext.Ajax.request({
					   url: Ext.user.baseUrl + '/User/data/',
					   method:'POST',
					   callback:function(o,s,r){
							var data = Ext.util.JSON.decode(r.responseText);
							var Record = Ext.data.Record.create([{name: 'account', type: 'string'}, 'did','roleid','nickname','email','datatype','info']);
							var dataform = Ext.getCmp('userForm');
							dataform.form.loadRecord(new Record(data));
					   },
					   params: {where:g.getSelectionModel().getSelected().id,do:'userid'}
					});

					Ext.getCmp('account').disable();
					Ext.getCmp('userForm').add(new Ext.form.Hidden({name:'id',value:this.edit,renderTo:'userFormid'}));
				}
			},
			viewConfig: {forceFit: true}
			}
        ];
		 this.bbar = new Ext.PagingToolbar({
            pageSize: Ext.sysconfig.pageSize,
            store: 'UserStore',
            displayInfo: true,
            displayMsg: Ext.sysconfig.pagemsg,
            emptyMsg: Ext.sysconfig.emptyList
        });
        Ext.user.userdatalist.superclass.initComponent.call(this);
    }
});

Ext.user.userliststoreWriter = new Ext.data.JsonWriter({
    encode: false
}); 

Ext.user.userproxy = new Ext.data.HttpProxy({
    api: {
			read : Ext.user.baseUrl +'/User/data/',
			destroy: Ext.user.baseUrl +'/User/delete/',
			update: Ext.user.baseUrl +'/User/status/',
		 }
});
//数据
Ext.user.userdata = Ext.extend(Ext.data.JsonStore, {
    listeners:{
		update:function(s,r,o){
			s.commitChanges();
		}
	},
    constructor: function(cfg) {
        cfg = cfg || {};
        Ext.user.userdata.superclass.constructor.call(this, Ext.apply({
            storeId: 'UserStore',
			writer:Ext.user.userliststoreWriter,
			proxy:Ext.user.userproxy,
            url: Ext.user.baseUrl + '/User/data/',
            autoLoad: true,
			root:'list',
			totalProperty: 'count',
			paramNames:{start:'page',limit:'limit'},
            fields: ['id','account','email','did','job','last_login_time','last_login_ip','login_count','create_time','status']
        }, cfg));
    }
});
